package com.redsoft.spirit.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import com.redsoft.spirit.entity.Menu;

/**
 * 菜单的DAO类
 * 
 * <pre>
 * 菜单的DAO类
 * </pre>
 * 
 * @author 杨雷
 * @since 1.0
 */
public interface MenuRepository extends BaseRepository<Menu, String>{

	List<Menu> findByCurrLevel(int i);

	List<Menu> findByParentId(String id);

	Menu findById(String id);

	@Query("select m from Menu m where m.id in(select mr.menuId from MenuRole mr where mr.roleId in (select ra.roleId from RoleAccount ra where ra.accountId = ?1)) order by m.display asc")
	List<Menu> getMenusByUser(String userId);

	@Query("SELECT m FROM Menu m WHERE m.id IN(SELECT mr.menuId FROM MenuRole mr WHERE mr.roleId IN (SELECT ra.roleId FROM RoleAccount ra WHERE ra.accountId = ?1)) AND m.parentId= ?2 order by m.display asc")
	List<Menu> getMenusByUser(String userId, String id);

	@Query(value ="SELECT m.id FROM sys_menu m WHERE m.id IN(SELECT mr.menu_id FROM sys_menu_role mr WHERE mr.role_id = ? and mr.indeterminate_flag ='0') order by m.display asc", nativeQuery= true)
	List<Menu> getMenuIdsByRole(String roleId);
}
